#ifndef GBFONT_H
#define GBFONT_H

#include "GBFreeType.h"
#include <string>

#ifndef GB_FONT_MAX_LENGTH
#define GB_FONT_MAX_LENGTH 512
#endif

class GBFont
{
private:
	int m_nID;
	long m_pt;
	std::string m_fontFace;
	static GBFreeType m_fonter;	// Every font will have a FreeType2 agent inside it (initialized only once)

public:
	GBFont();
	~GBFont();

	// Sets up a 3D Font
	bool BuildFont(const char* szFileName, long pt);

	// Caches the font in memory so every character is loaded into memory
	void Cache(unsigned char begin = ' ', unsigned char end = GB_NUM_CHARS) const { m_fonter.CacheFace(m_nID, begin, end); }

	// Destroys the font
	void KillFont();

	// Limited to: 256 bytes
	void Print(const char* szFmt, ...) const;

	// Returns name of current font
	const char* GetFace() const { return m_fontFace.c_str(); }
	long GetPtSize() const { return m_pt; }

	// GBFreetype interlaced accessors
	float GetGlyphWidth(const char ch) const { return m_fonter.GetGlyphWidth(m_nID, ch); }
	float GetGlyphHeight(const char ch) const { return m_fonter.GetGlyphHeight(m_nID, ch); }
	float GetTextWidth(const char* szText) const { return m_fonter.GetTextWidth(m_nID, szText); }
	float GetTextHeight(const char* szText) const { return m_fonter.GetTextHeight(m_nID, szText); }
	const GBFreeType::GBCharMesh* GetGlyphMesh(const char ch) const { return m_fonter.GetGlyphMesh(m_nID, ch); }
};

#include <deque>
class GBFontFactory	// SINGLETON
{
private:
	std::deque<GBFont> m_fonts;
	static GBFontFactory m_factory;

	GBFontFactory();
	~GBFontFactory() { ClearFonts(); }
	GBFontFactory operator=(const GBFontFactory&);
	GBFontFactory(const GBFontFactory&);

public:
	static GBFontFactory* Instance() { return &m_factory; }

	void ClearFonts();

	unsigned GetNumFonts() const { return (unsigned)m_fonts.size(); }

	// Allocates memory if the font is not found
	// Return: NULL, if font cannot be "searched"/found
	GBFont* GetFont(const char* szFontFace, long pt);
	GBFont* GetFont(const std::string& szFontFace, long pt) { return GetFont(szFontFace.c_str(), pt); }
};

#endif